<template>
    <div class="shopcar-container">
        <div class="shopcar-list">
            <div class="mui-card">
				<div class="mui-card-content">
					<div class="mui-card-content-inner" v-for="(item,i) in goodslist" :key="item.id">
						<mt-switch v-model="$store.getters.getGoodsSelected[item.id]" @change="selectChange(item.id,$store.getters.getGoodsSelected[item.id])"></mt-switch>
                        <img :src="item.thumb_path" alt="">
                        <div class="info" >
                            <h1>{{item.title}}</h1>
                            <p>
                                <span>￥{{item.sell_price}}</span>
                                <carnumbox style="height:25px;" :initCount="$store.getters.getEveryCount[item.id]" :goodid="item.id"></carnumbox>
                                <a href="" @click.prevent="remove(item.id,i)">删除</a>
                            </p>
                        </div>
					</div>
				</div>
			</div>
        </div>
        <div class="mui-card">
				<div class="mui-card-content">
					<div class="mui-card-content-inner check" >
						<div class="left">
                            <p>总计(不含运费)</p>
                            <p>已勾选商品<span>{{$store.getters.getCheckInfo["count"]}}</span>件，总价<span>{{$store.getters.getCheckInfo["amount"]}}</span>元</p>
                        </div>
                        <div class="right">
                            <mt-button type="danger">去结算</mt-button>
                        </div>
					</div>
				</div>
			</div>
            <p>{{$store.getters.getGoodsSelected}}</p>
            <p>{{$store.getters.getCheckInfo}}</p>
    </div>
</template>

<script>
import carnumbox from '../subcomponents/shopcarNumbox.vue'
export default {
    data(){
        return{
            goodslist:[]
        }
    },
    created(){
        this.getAllInfo();
    },
    methods:{
        getAllInfo(){
            //先获取本地的购物车里面的id
            var idlist = [];
            var id_count = {};
            this.$store.state.car.forEach(item => {
                idlist.push(item.id);
            });
            if(idlist.length<=0){
                return;
            }
            else{
                this.$http.get('http://www.liulongbin.top:3005/api/goods/getshopcarlist/'+ idlist.join(',')).then(result =>{
                    if(result.body.status === 0){
                        this.goodslist = result.body.message;
                    }
                })
            }        
        },
        remove(id,i){
            //id 用于删除 store里面的数据
            //i用于删除 goodslist里面的数据
            this.goodslist.splice(i);//删除选择的下标的数据
            this.$store.commit("removeFromCar",id);

        },
        selectChange(id,flag){
            //点击开关，将最新的开关的状态进行同步
            console.log(id+'--'+flag);
            this.$store.commit("updateSelectFlag",{id:id,Flag:flag})
        }
        
    },
    components:{
        carnumbox
    }
}
</script>

<style lang="scss" scoped>
div.shopcar-container{
    background-color: #ccc;
    overflow: hidden;
    .shopcar-list{
        .mui-card-content-inner{
            display: flex;
            align-items: center;
            img{
            width: 60px;
            height: 60px;
            }
            h1{
                font-size: 13px;
            }
            .info{
                P{margin-top:10px;}
                span{
                color: red;
                }
            }
            
        }
        
    }
    .check{
        display: flex;
        justify-content: space-between;
        overflow: hidden;
        .right{
            .mint-button{
                margin-top:8px;
            }
        }
        span{
            color:red;
        }
    }
}
</style>